//
//  ScrollViewMasonry.m
//  AFNScrollViewInMasonryBeta
//
//  Created by king on 15/12/1.
//  Copyright © 2015年 king. All rights reserved.
//

#import "ScrollViewMasonry.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "TestModel.h"

@implementation ScrollViewMasonry

-(id)initWithImageTimeInterval:(NSInteger)timeInterval {
    self = [super init];
    if (self) {
        // 创建scrollView
        [self createScrollView];
        // 创建容器
        [self createContenView];
        // 设置定时器时间
        self.timerScroll = timeInterval;
        // 加手势
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self.scrollView addGestureRecognizer:tap];
    }
    return self;
}
#pragma mark - Method set方法
- (void)setArr:(NSMutableArray *)arr {
    if (_arr != arr) {
        _arr = arr;
    }
    // 加载滚动视图图片
    [self loadImageNum];
    // 创建页码控制器
    [self createPage];
    // 设置定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timerScroll target:self selector:@selector(autoShowNext:) userInfo:@(arr.count) repeats:YES];
    self.num = 0;
}
#pragma mark - Method 创建滚动视图
-(void)createScrollView {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self);
        make.width.mas_equalTo(self.mas_width);
        make.height.mas_equalTo(self.mas_height);
    }];
}
#pragma mark - Method 创建容器
-(void)createContenView {
    self.contenView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contenView];
    self.contenView.backgroundColor = [UIColor redColor];
    [self.contenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView);
    }];
}
//图片已经预先改成ad0,ad1这种格式
-(void)loadImageNum {
    
    UIImageView *lastView = nil;
    for ( int i = 0 ; i < self.arr.count ; i ++) {
        UIImageView * subv = [[UIImageView alloc] init];
        TestModel *temp = self.arr[i];
        [subv sd_setImageWithURL:[NSURL URLWithString:temp.imgurl]] ;
        [self.contenView addSubview:subv];
        [subv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastView ? lastView.mas_right : @0);
            make.bottom.equalTo(@0);
            make.width.equalTo(self.scrollView.mas_width);
            make.height.equalTo(self.scrollView.mas_height);
        }];
        
        lastView = subv;
    }
    [self.contenView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(lastView.mas_right);
    }];
}

-(void)createPage {
    self.page = [[UIPageControl alloc] init];
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.page.currentPage = 0;
    self.page.numberOfPages = self.arr.count;
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    [self addSubview:self.page];
    [self.page mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom);
        make.centerX.equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(self.bounds.size.width/2 , 30));
    }];
    
}
#pragma mark - Method 定时器
-(void)autoShowNext:(NSTimer * )sender {
    
    NSInteger count = [sender.userInfo integerValue];
    
    [self.scrollView setContentOffset:CGPointMake(self.bounds.size.width*((int)(self.num)%count), 0) animated:YES];
    self.num++;
    
}
#pragma mark - Method 设置视图是否自动滚动
-(void)setAutoScroll:(BOOL)isScroll {
    int num = isScroll?1:0;
    switch (num)
    {
        case YES:
        {
            [self.timer fire];
        }
            break;
            
        case NO:
        {
            [self.timer invalidate];
        }
            break;
    }
}
-(void)layoutSubviews {
    [super layoutSubviews];
}
-(void)tap:(UITapGestureRecognizer * )tap {
//    [self.delegate adClick:self.page.currentPage];
}

- (void) scrollViewDidScroll:(UIScrollView *)sender {
    // 得到每页宽度
    CGFloat pageWidth = sender.frame.size.width;
    // 根据当前的x坐标和页宽度计算出当前页数
    int currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.page.currentPage = currentPage;
}


@end
